//
//  ADNetWorkingTool.swift
//  myBlog
//
//  Created by mac2014 on 16/3/22.
//  Copyright © 2016年 AD. All rights reserved.
//


/// 网络单例：继承AFNetWorking
import UIKit
import AFNetworking

let ADBaseURL = NSURL(string: "https://api.weibo.com/")

class ADNetWorkingTool: NSObject {
    //网络单例
    static let sharedInstance: ADNetWorkingTool = ADNetWorkingTool()
    
    private let netManager: AFHTTPSessionManager = AFHTTPSessionManager(baseURL: ADBaseURL)
    
    private override init() {
        super.init()
        //设置AFN支持类型：text/plain
        netManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }

    //自定义参数请求方法
    func GET(urlString: String, parameters: AnyObject, progress: ((NSProgress) -> Void)?, requestCallBack: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        netManager.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            //请求回来的数据为json格式（字典）
            let dict = result as? [String: AnyObject]
            requestCallBack(result: dict, error: nil)
            }) { (_, error) -> Void in
                requestCallBack(result: nil, error: error)
        }
    }
    
    func POST(urlString: String, parameters: AnyObject, progress: ((NSProgress) -> Void)?, requestCallBack: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        netManager.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            //请求回来的数据为json格式（字典）
            let dict = result as? [String: AnyObject]
            requestCallBack(result: dict, error: nil)
            }) { (_, error) -> Void in
                requestCallBack(result: nil, error: error)
        }
    }

}
